﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FACUI.BasicClass
{


    public class IniFile
    {
        private Dictionary<string, Dictionary<string, string>> iniCahce = new Dictionary<string, Dictionary<string, string>>();

        public IniFile(string INIFile)
        {
            this.FileName = INIFile;
        }

        public bool ContainSectionName(string secName)
        {
            return this.GetAllSectionName().Contains(secName);
        }

        public void DeleteSection(string section)
        {
            if (this.iniCahce.ContainsKey(section))
            {
                this.iniCahce.Remove(section);
            }
        }

        public void DeleteString(string section, string key)
        {
            if (this.iniCahce.ContainsKey(section) && this.iniCahce[section].ContainsKey(key))
            {
                this.iniCahce[section].Remove(key);
            }
        }

        public ICollection<string> GetAllSectionName()
        {
            return this.iniCahce.Keys;
        }

        public byte[] GetBytes()
        {
            StringBuilder builder = new StringBuilder(0xf4240);
            foreach (KeyValuePair<string, Dictionary<string, string>> pair in this.iniCahce)
            {
                builder.AppendLine("[" + pair.Key + "]");
                Dictionary<string, string> dictionary = pair.Value;
                foreach (KeyValuePair<string, string> pair2 in dictionary)
                {
                    builder.AppendLine(pair2.Key + "=" + pair2.Value);
                }
                builder.AppendLine("");
            }
            return Encoding.GetEncoding(0x3a8).GetBytes(builder.ToString());
        }

        public int GetInt(string section, string key, int defValue)
        {
            if (this.iniCahce.ContainsKey(section))
            {
                Dictionary<string, string> dictionary = this.iniCahce[section];
                if (dictionary.ContainsKey(key))
                {
                    string str = dictionary[key];
                    if (string.IsNullOrEmpty(str))
                    {
                        return defValue;
                    }
                    return Convert.ToInt32(str);
                }
            }
            return defValue;
        }

        private static string GetSecString(string str)
        {
            int length = str.Length;
            if ((str[0] == '[') && (str[length - 1] == ']'))
            {
                string str2 = str.Substring(1, length - 2);
                if (str2.StartsWith(";;") || str2.StartsWith("；；"))
                {
                    str2 = ";;";
                }
                return str2.Trim();
            }
            return "";
        }

        public ICollection<string> GetSectionItemName(string sectName)
        {
            if (this.iniCahce.ContainsKey(sectName))
            {
                Dictionary<string, string> dictionary = this.iniCahce[sectName];
                return dictionary.Keys;
            }
            return null;
        }

        public string GetString(string section, string key)
        {
            if (this.iniCahce.ContainsKey(section))
            {
                Dictionary<string, string> dictionary = this.iniCahce[section];
                if (dictionary.ContainsKey(key))
                {
                    return dictionary[key];
                }
            }
            return "";
        }

        public bool Load()
        {
            if (!File.Exists(this.FileName))
            {
                File.Create(this.FileName).Close();
                return false;
            }
            StreamReader rd = new StreamReader(this.FileName, Encoding.Default);
            if (rd == null)
            {
                return false;
            }
            Dictionary<string, string> dictionary = null;
            string str = "";
            bool flag = false;
            while (true)
            {
                str = this.ReadLine(rd);
                if (str == null)
                {
                    break;
                }
                str = str.Trim();
                if ((str != "") && ((str.Length <= 0) || ((str[0] != ';') && (str[0] != '#'))))
                {
                    string secString = GetSecString(str);
                    if (secString == ";;")
                    {
                        flag = false;
                    }
                    if ((secString != "") && (secString != ";;"))
                    {
                        flag = true;
                        if (this.iniCahce.ContainsKey(secString))
                        {
                            MessageBox.Show(secString + " 段重复, 请修改配置文件！");
                            break;
                        }
                        dictionary = new Dictionary<string, string>();
                        this.iniCahce.Add(secString, dictionary);
                    }
                    else if (flag && (dictionary != null))
                    {
                        string[] strArray = this.SplitKeyVal(str);
                        if ((strArray != null) && (strArray.Length >= 2))
                        {
                            strArray[0] = strArray[0].Trim();
                            int startIndex = strArray[1].LastIndexOf(";;");
                            if (startIndex != -1)
                            {
                                strArray[1] = strArray[1].Remove(startIndex).Trim();
                            }
                            else
                            {
                                strArray[1] = strArray[1].Trim();
                            }
                            if (!dictionary.ContainsKey(strArray[0]))
                            {
                                dictionary.Add(strArray[0], strArray[1]);
                            }
                        }
                    }
                }
            }
            rd.Close();
            return true;
        }

        private string ReadLine(StreamReader rd)
        {
            bool flag;
            string str = "";
        Label_007F:
            flag = true;
            string str2 = rd.ReadLine();
            if (str2 == null)
            {
                return null;
            }
            str2 = str2.Trim();
            if (str2 != "")
            {
                if (str2[str2.Length - 1] == '\\')
                {
                    str = str + str2.Substring(0, str2.Length - 1).Trim();
                    goto Label_007F;
                }
                str = str + str2;
            }
            return str;
        }

        public bool RemoveSection(string secName)
        {
            return this.iniCahce.Remove(secName);
        }

        public void Save()
        {
            try
            {
                StreamWriter writer = new StreamWriter(this.FileName, false, Encoding.Default);
                foreach (KeyValuePair<string, Dictionary<string, string>> pair in this.iniCahce)
                {
                    writer.WriteLine("[" + pair.Key + "]");
                    Dictionary<string, string> dictionary = pair.Value;
                    foreach (KeyValuePair<string, string> pair2 in dictionary)
                    {
                        writer.WriteLine(pair2.Key + "=" + pair2.Value);
                    }
                    writer.WriteLine("");
                }
                writer.Close();
            }
            catch (Exception exception)
            {
                Log.appendLog(exception.ToString());
            }
        }

        private string[] SplitKeyVal(string str)
        {
            int length = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '=')
                {
                    length = i;
                    break;
                }
            }
            if ((length > 0) && (length < str.Length))
            {
                return new string[] { str.Substring(0, length), str.Substring(length + 1) };
            }
            return null;
        }

        public void WriteInt(string section, string key, int val)
        {
            this.WriteString(section, key, val.ToString());
        }

        public void WriteString(string section, string key, string str)
        {
            if (!this.iniCahce.ContainsKey(section))
            {
                this.iniCahce.Add(section, new Dictionary<string, string>());
            }
            if (!string.IsNullOrEmpty(key))
            {
                Dictionary<string, string> dictionary = this.iniCahce[section];
                dictionary[key] = str;
            }
        }

        public string FileName { get; set; }
    }
}

